package com.cn.jx.manager.service.mtn.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.JobRepository;
import com.cn.jx.manager.form.JobForm;
import com.cn.jx.manager.po.Job;
import com.cn.jx.manager.service.mtn.IJobServiceMtn;
import com.jx.enumType.OperatEnum;
import com.jx.util.ObjectTransUtil;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("jobServiceMtn") 
@Transactional
public class JobServiceImplMtn extends BaseService implements IJobServiceMtn {
	@Autowired
	private JobRepository jobDao;
	
	/* 岗位的保存或更新功能 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key = "jobServiceQry", value = "'findAllJob'")
	public void saveOrUpdateJob(JobForm item) throws Exception {
		if (Utility.isNotBlank(item.getId())) {
			Job job = findEntityByPkid(item.getId(), Job.class);// 得到库中的数据
			Job uo = ObjectTransUtil.getUpdateObj(job, item);
			if (uo != null) {
				uo.setOperatTime(new Date());
				uo.setOperatUser(item.getOperatUser());
				Job jobs = jobDao.save(uo);
				// 删除缓存
//				redisUtil.hdelItem("jobServiceQry", "getJobById=" + jobs.getId());

				logUtil.LogMsg(Job.class, jobs.getId(), OperatEnum.UPDATE, item.getOperatUser());
			}
		} else {
			Job job = Job.getInstance(item.getOperatUser());
			job.setJobCode(item.getJobCode());
			job.setJobName(item.getJobName());
			job.setJobDescribe(item.getJobDescribe());
			Job jobs = jobDao.save(job);
			logUtil.LogMsg(Job.class, jobs.getId(), OperatEnum.SAVE, item.getOperatUser());
		}
	}

	/* 岗位的删除功能 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key = "jobServiceQry", value = "'findAllJob'")
	public void delJob(List<String> ids, String opter) throws Exception {
		deleteEntityByIds(ids, Job.class);
		logUtil.LogMsg(Job.class, Utility.getListToStr(ids), OperatEnum.DELETE, opter);
	}

	/* 彻底移除功能 此功能管理员使用。 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeJob(List<String> ids, String opter) throws Exception {
		removeEntityByIds(ids, Job.class);
		logUtil.LogMsg(Job.class, Utility.getListToStr(ids), OperatEnum.REMOVE, opter);
	}

	/* 回收站找回 需要清理查询所有job的缓存 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key = "jobServiceQry", value = "'findAllJob'")
	public void recoverJob(List<String> ids, String opter) throws Exception {
		recoverEntityById(ids, Job.class);
		logUtil.LogMsg(Job.class, Utility.getListToStr(ids), OperatEnum.RECOVER, opter);
	}

	/* 清空回收站 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeJobAll(String opertor) throws Exception {
		removeAllEntity(Job.class);
		// 保存日志
		logUtil.LogMsg(Job.class, "ALL", OperatEnum.REMOVE, opertor);
	}
}
